<template>
  <div>
    <a-card title="科目查询">
      <a-form :model="queryParams" layout="inline">
        <a-form-item label="名称">
          <a-input v-model:value="queryParams.name" />
        </a-form-item>
        <a-form-item label="类型">
          <a-select v-model:value="queryParams.type">
            <a-select-option value="income">收入</a-select-option>
            <a-select-option value="expense">支出</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="状态">
          <a-select v-model:value="queryParams.enabled" style="width: 80px">
            <a-select-option value="true">启用</a-select-option>
            <a-select-option value="false">停用</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" html-type="submit" @click="query">查询</a-button>
        </a-form-item>
        <a-form-item>
          <a-button @click="reset">重置</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-divider style="border-color: #7cb305" dashed />
    <a-card title="查询结果">
      <a-table
        :columns="columns"
        :data-source="accountingSubjectDataSet"
        rowKey="uid"
        :pagination="paginationConfig"
      >
        <template #action="{ record }">
          <a-button style="margin-right: 10px" @click="edit(record.uid)">修改</a-button>
          <a-button v-if="record.enabled" type="danger" @click="disable(record.uid)">停用</a-button>
          <a-button v-if="!record.enabled" type="primary" @click="enable(record.uid)"
            >启用
          </a-button>
        </template>
        <template #enabled="{ record }">
          <span v-if="record.enabled">启用</span>
          <span v-else>停用</span>
        </template>
        <template #type="{ record }">
          <span v-if="record.type == 'INCOME'">收入</span>
          <span v-else>支出</span>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useHttpRequest } from '@/api/chunfeng/apiRequest'
import { UnwrapNestedRefs } from '@vue/reactivity'
import {
  AccountingSubjectData,
  QueryAccountingSubjectData,
} from '@/api/chunfeng/accounting/interfaces'

import { useAccountingApi } from '@/api/chunfeng/accounting'

export default defineComponent({
  name: 'AccountingSubjectList',
  // components: {
  //   Card,
  //   Form
  // },
  setup() {
    const { executeQueryRequest, executeCommandRequest } = useHttpRequest()

    const { queryAccountingSubject, enableAccountingSubject, disableAccountingSubject } =
      useAccountingApi()
    const accountingSubjectDataSet = ref(Array<AccountingSubjectData>())

    const paginationConfig = reactive({
      showSizeChanger: true,
    })

    const queryParams: UnwrapNestedRefs<QueryAccountingSubjectData> = reactive({
      name: undefined,
      type: undefined,
      enabled: undefined,
    })

    // const { resetFields } = useForm(queryParams)
    const columns = [
      {
        title: '名称',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: '类型',
        dataIndex: 'type',
        key: 'type',
        slots: { customRender: 'type' },
      },

      {
        title: '状态',
        dataIndex: 'enabled',
        key: 'enabled',
        slots: { customRender: 'enabled' },
      },
      {
        title: '操作',
        key: 'action',
        slots: { customRender: 'action' },
      },
    ]

    function enable(uid: string) {
      executeCommandRequest(enableAccountingSubject(uid), {
        message: '科目启用成功',
        callback: () => {
          query()
        },
      })
    }

    function disable(uid: string) {
      executeCommandRequest(disableAccountingSubject(uid), {
        message: '科目停用成功',
        callback: () => {
          query()
        },
      })
    }

    function edit(uid: string) {}

    function reset() {}

    function query() {
      accountingSubjectDataSet.value.length = 0
      executeQueryRequest(
        queryAccountingSubject(queryParams),
        (results: AccountingSubjectData[]) => {
          accountingSubjectDataSet.value.push(...results)
        },
      )
    }

    return {
      queryParams,
      columns,
      enable,
      disable,
      query,
      accountingSubjectDataSet,
      paginationConfig,
      edit,
      reset,
    }
  },
})
</script>
